var currentTheme;
function setTheme(themeName) {
    //$('<link class="mainsheet" href="../resources/css/'+ themeName +'.css" rel="stylesheet" type="text/css" />').appendTo('head');
    var head = document.getElementsByTagName("head")[0];
    var link = document.createElement("link");
    link.href = '../resources/css/'+themeName+'.css';
    link.rel = "stylesheet";
    link.type = "text/css";
    link.className = "mainsheet";
    head.appendChild(link);
    if($(".mainsheet").length>1){
        $(".mainsheet:first").remove();
    }
    currentTheme = themeName;
};

var frameHeight;
function fullScreenToggle(){
	$("#bodyWrapper").toggleClass("fullScreened");
	$("#flexFrame").parents().toggleClass("isFullScreened");
	var height = $("#flexFrame").attr("height");
	if(height!="100%"){
		frameHeight = $("#flexFrame").height()
		$("#flexFrame").attr("height","100%");
	}else{
		$("#flexFrame").attr("height",frameHeight);
	}
	if($.browser.msie) {
		var wsh = new ActiveXObject('WScript.Shell');
		wsh.SendKeys('{F11}');
	}
}

function popupDialog(url,title,showCloseBtn){
	var divID = "popupDialog";
	$("body").append('<div id="'+divID+'"></div>');
	$.get(url,function(data){
		var dialog = $("#"+divID).html(data).dialog({
			autoOpen: true,
			modal: true,
			minWidth:500,
			title:title,
			close:function(){
				$("#"+divID).remove();
			}
		});
		if(showCloseBtn){
			dialog.dialog("option","buttons",{
				"关闭":function(){
					$(this).dialog("close");
				}
			});
		}
	});
}

//随机字符
function randomChar(l) {
    var x="123456789poiuytrewqasdfghjklmnbvcxzQWERTYUIPLKJHGFDSAZXCVBNM";
    var tmp="";
    for(var i=0;i< l;i++) {
        tmp += x.charAt(Math.ceil(Math.random()*100000000)%x.length);
    }
    return tmp;
}
$.fn.dropdown = function(settings){
	var options = $.extend({
		initialText: '下拉菜单',
		width: 150,
		height: 200,
		buttonHeight: 14,
		closeOnSelect: false,
		onOpen: function(){},
		onSelect: function(){},
		onClose: function(){},
		content: '菜单内容'
	}, settings);

	//菜单
	var randomClassName = randomChar(22);
	var button = $('<a class="dropdownTrigger c'+randomClassName+'" href="#"><span class="dropdownIcon"></span><span class="dropdownTitle">'+ options.initialText +'</span></a>');
	var ddCont = $('<div class="dropdownContent">'+ options.content +'</div>');
	$(this).append(button);
	
	$(".c"+randomClassName).click(function(){
		if(ddCont.is(':visible')){
			//ddCont.ddHide();
			ddCont.slideUp(50, function(){options.onClose();});
            $(".c"+randomClassName).css(button_default);
		}else{
			//ddCont.ddShow();
			ddCont.css({top: $(".c"+randomClassName).offset().top + options.buttonHeight + 6, left: $(".c"+randomClassName).offset().left}).slideDown(50);
            $(".c"+randomClassName).css(button_active);
            options.onOpen();
		}
		return false;
	});
	
	ddCont.hover(function(){
		
	},function(){
		if(ddCont.is(':visible')){
			//$(this).ddHide();
			ddCont.slideUp(50, function(){options.onClose();});
            $(".c"+randomClassName).css(button_default);
		}
	});

	//显示/隐藏
	// $.fn.ddShow = function(){
	    // alert("ddd");
		// //$(this).css({top: $(".c"+randomClassName).offset().top + options.buttonHeight + 6, left: $(".c"+randomClassName).offset().left}).slideDown(50);
		// $(this).slideDown(50);
		// $(".c"+randomClassName).css(button_active);
		// options.onOpen();
	// };
	// $.fn.ddHide = function(){
		// $(this).slideUp(50, function(){options.onClose();});
		// $(".c"+randomClassName).css(button_default);
	// };
	
	//css
	var button_default = {
		fontFamily: 'Trebuchet MS, Verdana, sans-serif',
		fontSize: '12px',
		color: '#666',
		background: '#eee url(../images/buttonbg.png) 50% 50% repeat-x',
		border: '1px solid #ccc',
		'-moz-border-radius': '6px',
		'-webkit-border-radius': '6px',
		textDecoration: 'none',
		padding: '3px 3px 3px 8px',
		width: options.width - 13, 
		display: 'block',
		height: options.buttonHeight,
		'line-height': options.buttonHeight+'px',
		outline: '0'
	};
	var button_hover = {
		'borderColor':'#bbb',
		'background': '#f0f0f0',
		cursor: 'pointer',
		color: '#444'
	};
	var button_active = {
		color: '#333',
		background: '#fff',
		border: '1px solid #ccc',
		borderBottom: 0,
		'-moz-border-radius-bottomleft': 0,
		'-webkit-border-bottom-left-radius': 0,
		'-moz-border-radius-bottomright': 0,
		'-webkit-border-bottom-right-radius': 0,
		outline: '0'
	};
	
	//按钮css
	$(".c"+randomClassName).css(button_default)
	.hover(function(){
		$(this).css(button_hover);
	},function(){
		if( !ddCont.is(':animated') && ddCont.is(':hidden') ){
			$(this).css(button_default);
		}
	})
	.find('.dropdownIcon').css({
		"float": 'right',
		width: '16px',
		height: '16px',
		background: 'url(../resources/images/icon_color_arrow.gif) 50% 50% no-repeat'
	});
	//内容css
	ddCont.css({
		position: 'absolute',
		"float": 'left',
		fontFamily: 'Trebuchet MS, Verdana, sans-serif',
		fontSize: '12px',
		background: '#fff',
		color: '#333',
		padding: '8px 3px 3px',
		border: '1px solid #ccc',
		'-moz-border-radius-bottomleft': '6px',
		'-webkit-border-bottom-left-radius': '6px',
		'-moz-border-radius-bottomright': '6px',
		'-webkit-border-bottom-right-radius': '6px',
		borderTop: 0,
		zIndex: 999999,
		width: options.width-8
	});
	
//	ddCont.find('a').click(function(){
//		alert("dsd");
//		var linkName = $(this).children().text();
//		alert(linkName);
//		button.find('.dropdownTitle').text(linkName);
//		options.onSelect();
//		if(options.closeOnSelect && ddCont.is(':visible')){
//			ddCont.ddHide();
//		}
//		return false;
//	});
	
	var ddcId ="#" + ddCont.find('[id]:first').attr("id");
	$('.dropdownContent:has('+ddcId+')').remove();
	$('body').append(ddCont);
	ddCont.hide();
	
	return this;
};

$(function(){
    
    var tab_counter = 3;
//  var gisTabIndex = 0;
//  $("ul.subMenu li a[href$='flex/index.jsp']").addClass("isTabed").attr("name","tabs-1");//gis已打开标记
    // tabs init with a custom tab template and an "add" callback filling in the content
    var $tabs = $( "#content").tabs({
        tabTemplate: "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close'>关闭标签</span></li>",
        idPrefix: 'tabs-',
        cache: true,
        remove: function(event, ui) {
            //这段代码用于清除实时曲线的interval定时刷新用的
            if(typeof(lineInterval)!='undefined') {
                clearInterval(lineInterval);
            }
            $('ul.subMenu li a[name='+ui.panel.id+'],a#updatePassword[name='+ui.panel.id+']').removeClass("isTabed");
            //gisTabIndex = $('#content ul li a[href="#tabs-1"]').parent().index();
            $("#siderFakeBGDiv").css("padding-top",$("ul.ui-tabs-nav").height());
        },
        add: function( event, ui ) {
            //gisTabIndex = $('#content ul li a[href="#tabs-1"]').parent().index();
            $tabs.tabs('select', '#' + ui.panel.id);
            $("#siderFakeBGDiv").css("padding-top",$("ul.ui-tabs-nav").height());
        }
    });
//  $(".ui-tabs-nav").sortable({delay:5});  //标签拖动
    
    // close icon: removing the tab on click
    // note: closable tabs gonna be an option in the future - see http://dev.jqueryui.com/ticket/3924
    $( "#content span.ui-icon-close" ).live( "click", function() {
        var index = $( "li", $tabs ).index( $( this ).parent() );
        $tabs.tabs( "remove", index );
    });
    
    $("ul.subMenu li a[href!='#']").click(function() {
        $('ul.subMenu li a').removeClass('menuHover');
        $(this).addClass('menuHover');
        
        if ($(this).hasClass("isTabed")){//判断是否已打开
            var nm = $(this).attr("name");
            if(nm=="tabs-1"){$tabs.tabs( "url" , gisTabIndex , this.href );}
            $tabs.tabs( "select" , "#"+nm );
            $tabs.tabs( "load" , "#"+nm );
        }else{
            $tabs.tabs("add", this.href, $(this).html());
            var nm = "tabs-"+tab_counter;
            //$tabs.tabs("select","#"+nm);
            $(this).addClass('isTabed').attr("name",nm);//已打开加标记
            tab_counter+=2;
        }
        return false;
    });
    
    $("#siderWrapper").css({
        "min-height":$(window).height()-56+"px"
    });
    //$(".subMenu").hide();
    $("#siderWrapper a,.ui-tabs .ui-tabs-nav li.ui-state-default a").each(function () {
        this.hideFocus = true;
        $(this).css("outline",0);
    });
    
    //侧栏收缩
    $("#collapse").click(function(){
        $(this).toggleClass("collapsed");
        $("#sider").toggleClass("collapsed");
        $("#pagebody").toggleClass("collapsed");
        $("#header").toggleClass("collapsed");
    });
    
    //菜单效果，链接处理
    $("#menuList ul li a[href='#']").click(function () {
        if($(this).hasClass("menuHover")){
            $(this).toggleClass('menuHover');
        }else{
            //$("#menuList ul li a[href='#']").removeClass('menuHover');
            $("ul.subMenu li a[href!='#']").removeClass('menuHover');
            $(this).addClass('menuHover');
        }
        //$(this).parent().siblings().find("ul").slideUp("fast");
        $(this).next().slideToggle("fast");
        return false;
    });
    
    
    //关闭按钮
    
    $(".close").live("click",function () {
        $(this).parent().fadeTo(400, 0, function () { // close关闭父窗口
            $(this).slideUp(400);
        });
        return false;
    });

    //checkbox全选,thead的checkbox
        
    $('.check-all').live("click",function(){
        $(this).parent().parent().parent().parent().find("input[type='checkbox']").attr('checked', $(this).is(':checked'));   
    });
        
    //容器头
        
    $(".cbHeader p").live("click",function () {
        $(this).parent().next().toggle();
        $(this).parent().parent().toggleClass("closedBox");
    });
    
    //关闭facebox
    $(".closeDialog").live("click",function(){
        $(this).parents("#popupDialog").dialog("close");
    });
    
    
});